<?php

/**
 * Created by PhpStorm.
 * User: shwy
 * Date: 2017/6/2
 * Time: 16:58
 * 供应商管理
 */
class supplier extends MY_Controller
{

    public function __construct()
    {

        parent::__construct();
        $this->load->model('supplier_model');

    }

    //供应商管理
    public function index()
    {
        $page_num = '4';//每页的数据
        $start_position = ''; //初始数据
        $page = ($this->input->get('page')); //获取页码数量
        if(!$page){
            $num = 1;
            $start_position = ($num - 1) * $page_num;
        }else{
            $start_position = ($page - 1) * $page_num;
        }

        //判断搜索内容是否存在存在就继续
        if($this->input->get('search')){
            $searchArg = [
                'field' => 'name',
                'arg'=> $this->input->get('search'),
                'side' => 'both'
            ];

            $data = $this->supplier_model->search('*',0,$searchArg,$page_num,$start_position);
            $viewData = [
                'list' => $data[0],  //这里得到从数据库中的当页数据
                'totalNum' => count($data['total_num']), //这里得到从数据库中的总页数
                'pageNum'  => $page_num //每页显示多少条
            ];
            $this->load->view('admin/supplier/list',$viewData);
        } else {
            $data = $this->supplier_model->page('supplier',0,$page_num,$start_position); //limit查询的数据
            $viewData = [
                'list' => $data[0],  //这里得到从数据库中的当页数据
                'totalNum' => $data['total_num'], //这里得到从数据库中的总页数
                'pageNum'  => $page_num //每页显示多少条

            ];

            $this->load->view('admin/supplier/list',$viewData);
        }
    }

    //新增功能
    public function add(){
        $data = [
            'code' => $this->input->post('code'),
            'name' => $this->input->post('name'),
            'simple' => $this->input->post('simple'),
            'linkman' => $this->input->post('linkMan'),
            'contactMan' => $this->input->post('contactMan'),
            'phone' => $this->input->post('phone'),
            'address' => $this->input->post('address'),
            'createTime' => $this->input->post('createTime'),
            'size' => $this->input->post('size'),
            'nature' => $this->input->post('nature'),
            'isProducers' => $this->input->post('isProducers'),
            'vender' => $this->input->post('vender'),
            'mail' => $this->input->post('mail'),
            'note' => $this->input->post('note'),
        ];
        //是否会有添加失败的情况?

        $resultData =  $this->supplier_model->add($data);

        if($resultData){
            echo '<script>alert("修改成功")</script>';
            redirect('/admin/supplier/index');
        }
    }

    //新增页面
    public function addView(){

        $this->load->view('/admin/supplier/add');
    }

    //编辑功能
    public function edit(){
        //用户id
//        print_r($_POST);
//        die();
        $where = [
            'id' => $this->input->post('id')
        ];
        //todo 更多字段
        $data = [
            'name' => $this->input->post('name'),
            'simple' => $this->input->post('simple'),
            'linkman' => $this->input->post('linkMan'),
            'contactMan' => $this->input->post('contactMan'),
            'phone' => $this->input->post('phone'),
            'address' => $this->input->post('address'),
            'createTime' => $this->input->post('createTime'),
            'size' => $this->input->post('size'),
            'nature' => $this->input->post('nature'),
            'isProducers' => $this->input->post('isProducers'),
            'vender' => $this->input->post('vender'),
            'mail' => $this->input->post('mail'),
            'note' => $this->input->post('note'),
        ];

//        print_r($data);
//        die();

        //是否会有添加失败的情况?
        $resultData =  $this->supplier_model->edit($data,$where);

//        print_r($resultData);
//        die();

        if($resultData){
            echo '<script>alert("修改成功")</script>';
            redirect('/admin/supplier/index');
        }

    }

    //编辑页面
    public function editView(){

        //读取用户名根据get id
        $arg = [
            'id' => $this->input->get('id')
        ];

        $data['list'] = $this->supplier_model->getByFieldQueryData('*',$arg,0);

//        print_r($data);
//        die();

        $this->load->view('/admin/supplier/edit',$data);
    }

    //删除功能
    public function remove(){
        $postData = $this->input->post();

        if ($postData){
            if(!$postData){
                redirect('/admin/supplier/index');
            }

            $post = $this->input->post('del_id');
            foreach ($post as $v){
                $where = [
                    'id' => $v
                ];

                $this->supplier_model->remove($where);
            }

            redirect('/admin/supplier/index');
        } else {
            $where = [
                'id' => $this->input->get('id')
            ];

            //是否会有添加失败的情况?
            $resultData =  $this->supplier_model->remove($where);
            if($resultData){
                echo '<script>alert("删除成功")</script>';
                redirect('/admin/supplier/index');
            }
        }
    }


    public function daochu(){

        $this->load->library('Excels');

        //表格标题
        $title = [
            0 => [
                0 => '序号',
                1 => '供应商编码',
                2 => '供应商名字',
                3 => '供应商简称',
                4 => '供应商联络人',
                5 => '供应商联系人',
                6 => '联系号码',
                7 => '地址',
                8 => '创建时间',
                9 => 'size',
                10 => 'nature',
                11 => 'isProducers',
                12 => 'vender',
                13 => 'mail',
                14 => 'note',
            ],
        ];

        $data = $this->supplier_model->getData('*',1);

        //表格内容
//        $content =  [
//            0 => [
//                0 => '序号',
//                1 => '供应商编码',
//                2 => '供应商名字',
//                3 => '供应商简称',
//                4 => '供应商联络人',
//                5 => '供应商联系人',
//                6 => '联系号码',
//                7 => '地址',
//                8 => '创建时间',
//                9 => 'size',
//                10 => 'nature',
//                11 => 'isProducers',
//                12 => 'vender',
//                13 => 'mail',
//                14 => 'note',
//        ],
//            1 => [
//                0 => '12312人',
//                1 => '所发生的丰富的',
//                2 => '积分高级人机'
//        ]];

        //二维数据
        $this->excels->exports($data,$title);

    }
    
    


}